<style scoped>
  .block {
    width: 148px;
    height: 148px;
    margin: 0 30px;
  }
</style>
<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['/indexSlideshow/insert']"
        >新增</el-button>
      </el-col>
    </el-row>

    <div style="width:100%;" v-if="indexSlideshowList">
      <div style="display:inline-block;"
        v-for="indexSlideshow in indexSlideshowList"
        :key="indexSlideshow.id"
      >
        <div  class="block">
          <img style="width:100%;height:100%;" class="el-upload-list__item-thumbnail"  alt="" v-if="indexSlideshow.type == eImgOrVideo.IMG.code" :src="indexSlideshow.img">
          <video style="width:100%;height:100%;"
            v-if="indexSlideshow.type == eImgOrVideo.VIDEO.code"
            :src="indexSlideshow.img"
            class="el-upload-list__item-thumbnail"
            controls="controls">
            您的浏览器不支持视频播放
          </video>
        </div>
        <div style="text-align:center;">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(indexSlideshow)"
            v-hasPermi="['/indexSlideshow/updateById']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(indexSlideshow)"
            v-hasPermi="['/indexSlideshow/deleteById']"
          >删除</el-button>
        </div>
      </div>
    </div>

    <!-- 添加或修改首页轮播图类型对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="文件" prop="img">
              <uploadImage :type="[eFileType.IMG,eFileType.VIDEO]" v-model="form.img"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="顺序" prop="sortNum">
              <el-input v-model="form.sortNum" placeholder="请输入顺序" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listIndexSlideshow, getIndexSlideshow, delIndexSlideshow, addIndexSlideshow, updateIndexSlideshow } from "@/api/indexSlideshow/info";
import UploadImage from '@/components/UploadImage';
import Editor from '@/components/Editor';
import { SORT_NUM } from "@/constant/constant";
import { FILE_TYPE, IMG_OR_VIDEO } from "@/enum/enum";

export default {
  name: "IndexSlideshow",
  components: {
    UploadImage,
    Editor
  },
  data() {
    return {
      // 枚举属性
      // 文件类型
      eFileType: FILE_TYPE,
      // 图片还是视频
      eImgOrVideo: IMG_OR_VIDEO,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 首页轮播图表格数据
      indexSlideshowList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        img: [
          { required: true, message: "文件不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询首页轮播图列表 */
    getList() {
      this.loading = true;
      listIndexSlideshow(this.queryParams).then(response => {
        this.indexSlideshowList = response.data;
        this.indexSlideshowList.forEach(element => {
          element.img = process.env.VUE_APP_STATIC_FILE_URL + element.img;
        });
        this.total = response.page.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        img: undefined,
        type: undefined,
        sortNum: SORT_NUM
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加首页轮播图";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getIndexSlideshow(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改首页轮播图";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateIndexSlideshow(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addIndexSlideshow(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除首页轮播图编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delIndexSlideshow(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    }
  }
};
</script>